package com.cloudcentral.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloudcentral.entity.Platform;
import com.cloudcentral.entity.dto.EnableDTO;
import com.cloudcentral.entity.query.PlatformQuery;
import com.cloudcentral.entity.vo.PageVO;
import com.cloudcentral.exception.FailureException;
import com.cloudcentral.service.PlatformService;
import com.cloudcentral.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统平台表 前端控制器
 * </p>
 *
 * @author Administrator
 * @since 2023-10-27
 */
@Slf4j
@RestController
@RequestMapping("/platform")
@Tag(name = "平台管理")
public class PlatformController extends BaseController {
  private final PlatformService platformService;

  @Autowired
  public PlatformController(PlatformService platformService) {
    this.platformService = platformService;
  }

  @PostMapping("/enable")
  @Operation(summary = "启用/禁用")
  public Result<Boolean> enable(EnableDTO dto) throws FailureException {
    return Result.ok(platformService.enable(dto));
  }

  @PostMapping("/submit")
  @Operation(summary = "提交")
  public Result<Platform> submit(Platform platform) throws Exception {
    return Result.ok(platformService.submit(platform));
  }

  @DeleteMapping("/delete")
  @Operation(summary = "删除")
  public Result<Boolean> delete(Long id) throws Exception {
    return Result.ok(platformService.delete(id));
  }

  @GetMapping("/select")
  @Operation(summary = "查询")
  public Result<Platform> select(Long id) {
    return Result.ok(platformService.getById(id));
  }

  @GetMapping("/list")
  @Operation(summary = "集合")
  public Result<List<Platform>> list(PlatformQuery query) {
    QueryWrapper<Platform> queryWrapper = new QueryWrapper<>();
    if (StringUtils.isNotBlank(query.getKeywords())) {
      String keywords = query.getKeywords();
      queryWrapper.like("name", keywords).or().like("code", keywords).or().like("uri", keywords).or().like("remark", keywords);
    }
    if (null != query.getEnable()) queryWrapper.eq("enable", query.getEnable() ? 1 : 0);
    return Result.ok(platformService.list(queryWrapper));
  }

  @GetMapping("/listPage")
  @Operation(summary = "分页列表")
  public Result<PageVO<Platform>> listPage(PlatformQuery query) {
    QueryWrapper<Platform> queryWrapper = new QueryWrapper<>();
    if (StringUtils.isNotBlank(query.getKeywords())) {
      String keywords = query.getKeywords();
      queryWrapper.like("name", keywords).or().like("code", keywords).or().like("uri", keywords).like("remark", keywords);
    }
    if (null != query.getEnable()) queryWrapper.eq("enable", query.getEnable() ? 1 : 0);
    IPage<Platform> iPage = platformService.page(new Page<>(query.getPage(), query.getSize()), queryWrapper);
    return Result.ok(new PageVO<>(iPage.getRecords(), iPage.getTotal()));
  }
}
